package work.xiaohong.storage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.storage.entity.StorageTbl;
import work.xiaohong.storage.service.StorageTblService;

import java.util.Arrays;
import java.util.List;


@Tag(name = "StorageTbl接口文档")
@RestController
@RequestMapping("/storage")
@CrossOrigin
public class StorageTblController {

    @Autowired
    private StorageTblService storageTblService;

    /***
     * 条件分页查询
     * @param storageTbl
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "StorageTbl对象",required = true) StorageTbl storageTbl, @PathVariable  int page, @PathVariable  int size){
        //调用StorageTblService实现分页条件查询StorageTbl
        Page pageInfo = storageTblService.findPage(storageTbl, page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 分页查询
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用StorageTblService实现分页查询StorageTbl
        Page pageInfo = storageTblService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 条件查询
     * @param storageTbl
     * @return
     */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<StorageTbl>> findList(@RequestBody(required = false) @Parameter(name = "StorageTbl对象",required = true) StorageTbl storageTbl){
        //调用StorageTblService实现条件查询StorageTbl
        List<StorageTbl> list = storageTblService.findList(storageTbl);
        return new Result<List<StorageTbl>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }

    /***
     * 根据Id删除storageTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除StorageTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用StorageTblService实现根据ID删除
        storageTblService.delete(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }

    /***
     * 根据Id批量删除storageTbl数据
     * @param ids
     * @return
     */
    @Operation(summary = "根据Id批量删除StorageTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的StorageTbl对象id集合",required = true) Long[] ids){
        //调用StorageTblService实现根据ID批量删除
        storageTblService.deleteByIds(Arrays.asList(ids));
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
     * 根据ID修改StorageTbl数据
     * @param storageTbl
     * @return
     */
    @Operation(summary = "根据ID修改StorageTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "StorageTbl对象",required = true) StorageTbl storageTbl){
        //调用StorageTblService实现修改StorageTbl
        storageTblService.updateById(storageTbl);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }

    /***
     * 新增StorageTbl数据
     * @param storageTbl
     * @return
     */
    @Operation(summary = "添加StorageTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "StorageTbl对象",required = true) StorageTbl storageTbl){
        //调用StorageTblService实现添加StorageTbl
        storageTblService.add(storageTbl);
        return new Result(true,StatusCode.OK.getCode(),"添加成功");
    }

    /***
     * 根据ID查询StorageTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询StorageTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<StorageTbl> findById(@PathVariable Integer id){
        //调用StorageTblService实现根据ID查询StorageTbl
        StorageTbl storageTbl = storageTblService.findById(id);
        return new Result<StorageTbl>(true,StatusCode.OK.getCode(),"查询成功",storageTbl);
    }

    /***
     * 查询StorageTbl全部数据
     * @return
     */
    @Operation(summary = "查询所有StorageTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<StorageTbl>> findAll(){
        //调用StorageTblService实现查询所有StorageTbl
        List<StorageTbl> list = storageTblService.findAll();
        return new Result<List<StorageTbl>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }
}
